package com.wonking.springbootv2.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.wonking.springbootv2.dao.UserDao;
import com.wonking.springbootv2.entity.User;
import com.wonking.springbootv2.service.UserService;

/**** imports ****/
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao = null;

	@Override
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 1)
	public int insertUser(User user) {
		userDao.insertUser(user);
		// int i=0;
		// int j= 3/i;
		return 0;
	}

	@Override
	//@Cacheable(value = "redisCache", key = "'redis_user_'+#id")
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 1)
	public User getUser(Long id) {
		System.out.println("数据库读取id=====" + id);
		return userDao.getUser(id);
	}

	// 移除缓存
	@Override
	@Transactional
	@CacheEvict(value = "redisCache", key = "'redis_user_'+#id", beforeInvocation = false)
	public int deleteUser(Long id) {
		return 0;//只是测试
	}
}